package com.dayouzi.crawler_monitor.service.impl;

import com.dayouzi.crawler_monitor.base.exception.BusinessException;
import com.dayouzi.crawler_monitor.config.DataSource;
import com.dayouzi.crawler_monitor.mapper.AnalysisMapper;
import com.dayouzi.crawler_monitor.pojo.entity.Crawler;
import com.dayouzi.crawler_monitor.pojo.entity.CrawlerDetail;
import com.dayouzi.crawler_monitor.pojo.page.PageBean;
import com.dayouzi.crawler_monitor.pojo.query.CrawlerListQuery;
import com.dayouzi.crawler_monitor.pojo.vo.AnalyData;
import com.dayouzi.crawler_monitor.pojo.vo.AnalysisListVO;
import com.dayouzi.crawler_monitor.pojo.vo.CrawlerListVO;
import com.dayouzi.crawler_monitor.pojo.vo.OptionDataVO;
import com.dayouzi.crawler_monitor.service.AnalysisService;
import com.dayouzi.crawler_monitor.utils.DateUtils;
import com.github.pagehelper.PageHelper;
import com.jcraft.jsch.MAC;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
@DataSource(value = "onlyforcrawler")
public class AnalysisServiceImpl implements AnalysisService {

    @Autowired
    private AnalysisMapper analysisMapper;


    @Override
    public HashMap<String, int[]> getOptionBySevenDay(Integer id, String date) {
        if (date.equals("")) {
            throw new BusinessException("日期不能为空");
        }
        // 查询聚合操作的数据
        List<AnalyData> analyData = analysisMapper.getOptionBySevenDay(id, date);

        // 结果容器
        HashMap<String, int[]> map = new HashMap<>();
        // 数组大小
        int arrSize = (int) ChronoUnit.DAYS.between(LocalDate.parse(date), LocalDate.now()) + 1;
        // 当前遍历的索引
        int index = -1;
        // 当前遍历的日期
        String cacheDay = date;

        // 遍历获取每种类型每天的数量
        for (AnalyData item : analyData) {
            if (!cacheDay.equals(item.getDate())) {
                // 如果不是同一天
                // 判断缓存的日期和当前遍历的日期相差多少天
                int days = (int) ChronoUnit.DAYS.between(LocalDate.parse(cacheDay), LocalDate.parse(item.getDate()));
                cacheDay = item.getDate();
                index += days;
            }

            if (map.containsKey(item.getType())) {
                // 如果存在type类型
                int[] arr = map.get(item.getType());
                arr[index] = item.getCount();
            } else {
                // 如果不存在type类型
                int[] arr = new int[arrSize];
                arr[index] = item.getCount();
                map.put(item.getType(), arr);
            }
        }

        // 计算除success类型以外的所有类型的和
        int[] errArr = new int[arrSize];
        for (int i = 0; i < arrSize; i++) {
            for (String key : map.keySet()) {
                if (!key.equals("success")) {
                    errArr[i] += map.get(key)[i];
                }
            }
        }
        map.put("errors", errArr);
        return map;
    }

    @Override
    public HashMap<String, int[]> getOptionBy24Hour(Integer id, String date) {
        if (date.equals("")) {
            throw new BusinessException("日期不能为空");
        }

        // 查询聚合操作的数据
        List<AnalyData> analyData = analysisMapper.getOptionBy24Hour(id, date);
        // 结果容器
        HashMap<String, int[]> map = new HashMap<>();
        // 数组大小
        int arrSize = 24;
        // 当前遍历的索引
        int index = 0;
        // 当前遍历的小时，借用AnalyData中的date字段放hour字段
        String cacheDay = "0";

        // 遍历获取每种类型每小时的数量
        for (AnalyData item : analyData) {
            if (!cacheDay.equals(item.getDate())) {
                // 如果不是同一小时
                // 判断缓存的小时和当前遍历的小时相差多少小时
                int hours = Math.abs(Integer.parseInt(item.getDate()) - Integer.parseInt(cacheDay));
                cacheDay = item.getDate();
                index += hours;
            }

            if (map.containsKey(item.getType())) {
                // 如果存在type类型
                int[] arr = map.get(item.getType());
                arr[index] = item.getCount();
            } else {
                // 如果不存在type类型
                int[] arr = new int[arrSize];
                arr[index] = item.getCount();
                map.put(item.getType(), arr);
            }
        }

        // 计算除success类型以外的所有类型的和
        int[] errArr = new int[arrSize];
        for (int i = 0; i < arrSize; i++) {
            for (String key : map.keySet()) {
                if (!key.equals("success")) {
                    errArr[i] += map.get(key)[i];
                }
            }
        }
        map.put("errors", errArr);
        return map;
    }


    @Override
    public PageBean<AnalysisListVO> listPage(Integer page, Integer limit, CrawlerListQuery crawlerListQuery) {
        // 分页查询
        PageHelper.startPage(page, limit);
        List<AnalysisListVO> list = analysisMapper.listPage(crawlerListQuery);
        return new PageBean<>(list);
    }

    @Override
    public List<HashMap<String, Integer>> getOptionByPieClass(Integer id, String date) {
        return analysisMapper.getOptionByPieClass(id, date);
    }

    @Override
    public List<HashMap<String, Integer>> getOptionByPieStatus(Integer id, String date) {
        return analysisMapper.getOptionByPieStatus(id, date);
    }

    @Override
    public Crawler getCrawlerDetail(Integer id) {
        return analysisMapper.getCrawlerDetail(id);
    }
}
